<template>
  <view class="search-box">
    <uni-search-bar v-on:input="input" v-bind:radius="100" cancel-button="none"></uni-search-bar>
  </view>
  <!-- 搜索建议列表 -->
  <view class="sugg-list" v-if="searchResults.length != 0">
    <view class="sugg-item" v-for="(item, index) in searchResults" v-bind:key="index" v-on:click="gotoDetail(item)">
      <view class="good-name">
        {{item.goods_name}}
      </view>
      <uni-icons type="arrowright" size="16"></uni-icons>
    </view>
  </view>
  
  <!-- 搜索历史 -->
  <view class="history-box" v-else>
    <!-- 标题 -->
    <view class="history-title">
      <text>搜索历史</text>
      <uni-icons type="trash" size="17" v-on:click="clean"></uni-icons>
    </view>
    <!-- 列表 -->
    <view class="history-list">
      <uni-tag v-bind:text="item" v-for="(item, index) in newHistories" v-bind:key="index" v-on:click="gotoGoodsList(item)"></uni-tag>
    </view>
  </view>
</template>

<script>
  import uniSearchBar from '@/uni_modules/uni-search-bar/components/uni-search-bar/uni-search-bar.vue';
  import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
  import uniTag from '@/uni_modules/uni-tag/components/uni-tag/uni-tag.vue'
  
  export default {
    data() {
      return {
        timer: 0,
        keyWord: '',   //搜索关键词
        searchResults: [],
        histories: []     //搜索历史的数组
      };
    },
    onLoad() {
        this.histories = JSON.parse(uni.getStorageSync('historicRecords') || '[]');
    },
    methods: {
      input(e){
        clearTimeout(this.timer);
        this.timer = setTimeout(() => {
          this.keyWord = e;
          this.getSearchList();
        }, 500);        
      },
      
      async getSearchList(){
        if(this.keyWord == ''){
          this.searchResults = [];
          return;
        }
        const res = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.keyWord});
        if(res.data.meta.status != 200){
          return uni.$showMessage()
        }
        console.log('res', res);
        this.searchResults = res.data.message;
        
        //保存搜索关键词
        this.saveSearchHistory();
      },
      
      gotoDetail(item){
        uni.navigateTo({
          url: '/subpkg/good_list/good_list?goods_id=' + item.goods_id
        })
      },
      saveSearchHistory(){
        //this.histories.push(this.keyWord);
        const setObj = new Set(this.histories);
        setObj.delete(this.keyWord);
        setObj.add(this.keyWord);
        //Array.from(): 将一个类数组对象或者可遍历对象转换为一个真正的数组
        this.histories = Array.from(setObj);
        //调用uni.setStorageSync(key,value)将搜索历史记录存到本地
        uni.setStorageSync('historicRecords', JSON.stringify(this.histories));
      },
      clean(){
        this.histories = [];
        uni.setStorageSync('historicRecords', []);
      },
      gotoGoodsList(item){
        uni.navigateTo({
          url: '/subpkg/good_list/good_list?query='+item
        })
      }
    },
    computed: {
      newHistories() {
        return [...this.histories].reverse();
      }
    }
  }
</script>

<style lang="scss">  
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  
  .sugg-list {
    padding: 0 5px;
    //background-color: #C00000;
    .sugg-item{
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      
      .good-name{
        //文本不允许换行(单行文本)
        white-space: nowrap;
        //溢出部分隐藏
        overflow: hidden;
        //文本溢出后, 使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
  
  .history-box {
    padding: 0 5px;
    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef
    }
    .history-list{
      display: flex;
      flex-wrap: wrap;
      .uni-tag{
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
